<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" auto-complete="on" label-position="left" size="default">
      <div class="login_box">
        <div class="top_box">
          <div class="logo">
            <img src="@/assets/images/logo.png" alt="" class="logo_img">
            客户管理系统
          </div>
          <div class="top_right" />
        </div>
        <div class="bottom_box">
          <div class="b_left">
            <el-form-item prop="phone">
              <el-input v-model="loginForm.phone" maxlength="11" placeholder="请输入手机号" type="text" />
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" @keyup.enter.native="handleLogin" />
            </el-form-item>
            <el-form-item>
              <div class="code_box">
                <el-input v-model="loginForm.code" placeholder="请输入验证码" name="password" @keyup.enter.native="handleLogin" />
                <el-button type="primary" class="code_btn" :disabled="time > 0" @click="getCode()">{{ time > 0 ? time + "后获取" : "发送验证码" }}</el-button>
              </div>

            </el-form-item>
            <el-button :loading="loading" type="primary" size="default" style="width: 100%; margin-bottom: 30px" @click="handleLogin">登录</el-button>
          </div>
          <div class="b_right" />
        </div>
      </div>
    </el-form>
  </div>
</template>
<script>
import { login } from "@/api/user";
import { PHONE_REG } from "@/const";
import { GET_PHONE_CODE } from "@/http/login";
import { showToast } from "@/utils/util";
export default {
  name: "Login",
  data() {
    const validatephone = (rule, value, callback) => {
      if (!PHONE_REG.test(value)) {
        callback(new Error("请输入正确的手机"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码长度必须大于6位"));
      } else if (value.length > 12) {
        callback(new Error("密码长度不能超过12位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        
      },
      loginRules: {
        phone: [{ required: true, trigger: "blur", validator: validatephone }],
        password: [
          { required: true, trigger: "blur", message: "请输入密码" },
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      loading: false,
      passwordType: "password",
      time: "",
    };
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    async handleLogin() {
      await this.$refs.loginForm.validate();
      let oParams = {};
      oParams.phone = this.loginForm.phone;
      oParams.password = this.loginForm.password;
      const res = await login(oParams, this.loginForm.code);
      if (!res.result) return;
      this.$message.success("登录成功!");
      window.location.href = "/";
    },

    // 获取验证码
    getCode() {
      let that = this;
      let o = this.loginForm;
      if (!o.phone) {
        showToast({
          message: "请输入手机号码",
        });
        return false;
      }

      if (!PHONE_REG.test(o.phone)) {
        showToast({
          message: "请输入正确手机号码",
        });
        return false;
      }
      let oParams = {};
      oParams.phone = o.phone;

      let time = 60;
      let m = setInterval(function () {
        if (time == 0) {
          clearInterval(m);
          that.time = "";
          return false;
        }
        time--;
        that.time = time;
      }, 1000);
      GET_PHONE_CODE(oParams).then((res) => {
        if (res.code == 200) {
          showToast({
            message: res.message,
            type: "success",
          });
        }
      });
    },
  },
};
</script>
<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;
</style>
<style lang="scss" scoped>
.login-container {
  background: #283443;
  height: 100vh;
}
.login_box {
  background: #ffffff;
  box-shadow: 0 8px 20px 0 rgba(0, 0, 0, 0.06);
  border-radius: 8px;
  border-radius: 8px;
  width: 720px;
  height: 400px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 20px;
}
.top_box {
  display: flex;
  .logo {
    flex: 1;
    font-size: 20px;
    color: #333333;
    line-height: 60px;
    font-weight: 500;
    display: flex;
  }
  .top_right {
    flex: 1;
    background: url("../../assets/images/point.png") no-repeat right center;
  }
}
.bottom_box {
  display: flex;
  padding: 60px 0 0 0;
  .b_left {
    flex: 1;
  }
  .b_right {
    flex: 1;
    background: url("../../assets/images/login_bg.png") no-repeat center center;
    background-size: contain;
    padding: 10px;
  }
}
.code_box {
  display: flex;
  justify-content: space-between;
  .code_btn {
    margin-left: 10px;
  }
}
</style>
